package com.itheima.controller;

import com.itheim.client.pojo.Order;
import com.itheima.common.R;
import com.itheima.common.TokenHolder;
import com.itheima.service.COrderService;
import com.itheima.util.RequestParesUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author longbosheng
 * @Date 2022/6/19 18:55
 * @PackageName:com.itheima.controller
 * @ClassName: OrderController
 * @Description: TODO
 * @Version 1.0
 */
@Log4j2
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private COrderService corderService;

    /**
     * 订单提交
     *
     * @return
     */
    @PostMapping("/submit")
    public R submitOrder(@RequestBody Order order, HttpServletRequest request) {
        String userId = RequestParesUtil.getRequestId(request);
        order.setUserId(userId);
        boolean result = corderService.addOrder(order);
        System.out.println("订单信息：" + order);
        if (result) {
            return R.success("成功提交");
        }
        return R.error("提交失败");
    }

    /**
     * 订单列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R orderList(int page, int pageSize) {
        String userId = TokenHolder.getCurrentId();
        return corderService.orderList(page, pageSize, userId);
    }
}
